package com.app.shop.activitys;

import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.app.shop.R;
import com.app.shop.adapters.OrderListAdapter;
import com.app.shop.beans.OrderBean;
import com.app.library.base.BaseActivity;
import com.app.library.utils.DialogUtil;
import com.app.library.utils.UserManager;
import com.app.library.widgts.CustomDialog;

import com.chad.library.adapter.base.BaseQuickAdapter;

import org.litepal.crud.DataSupport;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class OrderListActivity extends BaseActivity {


    private   TextView tvTitle;
    private   RecyclerView recy;

    List<OrderBean> list = new ArrayList<>();

    OrderListAdapter adapter;

    LinearLayout llTop;

    private String dialog_title = "确认";
    private String dialog_content = "是否确认操作？";
    private CustomDialog customDialog;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list);
        tvTitle= (TextView) findViewById(R.id.tv_title);
        recy= (RecyclerView) findViewById(R.id.recy);
        llTop= (LinearLayout) findViewById(R.id.ll_top);
        ImageView imgvreturn= (ImageView) findViewById(R.id.imgv_return);
        Button bt1= (Button) findViewById(R.id.bt_1);
        Button bt2= (Button) findViewById(R.id.bt_2);
        imgvreturn.setOnClickListener(this::onViewClicked);
        bt1.setOnClickListener(this::onViewClicked);
        bt2.setOnClickListener(this::onViewClicked);

        tvTitle.setText("订单列表");
        llTop.setVisibility(View.VISIBLE);
        initData();
    }


    private void initData() {
        list.clear();
        List<OrderBean> list_all = DataSupport.findAll(OrderBean.class);
        for (int i = 0; i < list_all.size(); i++) {
            if (list_all.get(i).getUser_id().equals(UserManager.getUserId(this))) {
                list.add(list_all.get(i));
            }
        }
        Collections.reverse(list);
        adapter = new OrderListAdapter(R.layout.item_order_list, list);
        adapter.setEmptyView(LayoutInflater.from(this).inflate(R.layout.view_list_empty, null));
        recy.setLayoutManager(new LinearLayoutManager(this));
        recy.setAdapter(adapter);
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Bundle bundle = new Bundle();
                bundle.putSerializable("bean", list.get(position));
                showActivity(OrderListActivity.this, OrderDetailActivity.class, bundle);
            }
        });
        adapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                del(position);
            }
        });
    }

    private void del(int position) {
        customDialog = DialogUtil.showDialog(this, customDialog, 2, dialog_title, dialog_content, "取消", "确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        }, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                DataSupport.deleteAll(OrderBean.class, "order_id=?", list.get(position).getOrder_id());
                list.remove(position);
                adapter.notifyDataSetChanged();
            }
        });

        if (customDialog != null && !customDialog.isShowing()) {
            customDialog.show();
        }


        customDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                customDialog = null;
            }
        });
    }


    public void onViewClicked(View view) {
        Bundle bundle = new Bundle();
        switch (view.getId()) {
            case R.id.imgv_return:
                onBackPressed();
                break;
            case R.id.bt_1:
                bundle.putInt("from", 1);
                showActivity(this, ZHListActivity.class, bundle);
                break;
            case R.id.bt_2:
                bundle.putInt("from", 2);
                showActivity(this, ZHListActivity.class, bundle);
                break;
        }
    }
}
